package me.zhengjie.util;

import lombok.Data;
import me.zhengjie.util.ruoyiexcel.StringUtils;

/**
 * @author Long
 * @date 2021/6/28 12:08
 */
@Data
public class BaseResponse<T> {
    private Boolean success = true;
    private String msg = "操作成功";
    private T data;

    public BaseResponse(Boolean success, String msg, T data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(String msg) {
        this.msg = msg;
    }

    public static BaseResponse ok(String msg) {
        if (msg != null && msg != "") {
            return new BaseResponse(msg);
        }
        return new BaseResponse("操作成功");
    }

    public static<T> BaseResponse<T> ok(T obj) {
        if(obj instanceof String){
            if(StringUtils.isNotBlank((String)obj)){
                return new BaseResponse(obj);
            }
        }
        if (obj != null) {
            return new BaseResponse(obj);
        }
        return new BaseResponse();
    }

    public static<T> BaseResponse<T> ok(String msg, T data) {
        BaseResponse baseResponse = new BaseResponse();
        if (StringUtils.isNotBlank(msg)) {
            baseResponse.setMsg(msg);
        }
        if (data != null) {
            baseResponse.setData(data);
        }
        return baseResponse;
    }

    public static BaseResponse ok() {
        return new BaseResponse();
    }

    public static BaseResponse failed(String msg) {
        if (msg != null && msg != "") {
            BaseResponse baseResponse = new BaseResponse(msg);
            baseResponse.setSuccess(false);
            return baseResponse;
        }
        BaseResponse baseResponse = new BaseResponse("操作失败");
        baseResponse.setSuccess(false);
        return baseResponse;
    }

    public static BaseResponse failed() {
        BaseResponse baseResponse = new BaseResponse("操作失败");
        baseResponse.setSuccess(false);
        return baseResponse;
    }

    public BaseResponse(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(T data) {
        this.data = data;
    }

    public BaseResponse() {
    }

}
